#include "LNSFAIoT_DO.h"


float SaturationDoTemperature = 25.0;    // 默认温度25℃
double SaturationDoVoltage = 1127.6;     // 默认饱和溶解氧电压:1127.6mv（需确认传感器规格）

// 不同温度下的饱和溶解氧浓度表(单位: mg/L)
const float SaturationValueTab[41] = {      
14.46, 14.22, 13.82, 13.44, 13.09,
12.74, 12.42, 12.11, 11.81, 11.53,
11.26, 11.01, 10.77, 10.53, 10.30,
10.08, 9.86,  9.66,  9.46,  9.27,
9.08,  8.90,  8.73,  8.57,  8.41,
8.25,  8.11,  7.96,  7.82,  7.69,
7.56,  7.43,  7.30,  7.18,  7.07,
6.95,  6.84,  6.73,  6.63,  6.53,
6.41,
};

LNSFAIoT_DO::LNSFAIoT_DO()
{
  
}

int getMedianNum(int bArray[], int iFilterLen) 
{
    int bTab[iFilterLen];
    for (byte i = 0; i < iFilterLen; i++)
    {
        bTab[i] = bArray[i];
    }
    int i, j, bTemp;
    for (j = 0; j < iFilterLen - 1; j++) 
    {
        for (i = 0; i < iFilterLen - j - 1; i++) 
        {
            if (bTab[i] > bTab[i + 1]) 
            {
                bTemp = bTab[i];
                bTab[i] = bTab[i + 1];
                bTab[i + 1] = bTemp;
            }
        }
    }
    if ((iFilterLen & 1) > 0)
        bTemp = bTab[(iFilterLen - 1) / 2];
    else
        bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
    return bTemp;
}

float LNSFAIoT_DO::readDO(int pin, float temperature)
{
    // const int ArrayLenth = 10;  // 定义数组长度（原代码可能在头文件中定义）
    int val[ArrayLenth] = {0};
    float doValue;
    
    // 读取多次ADC值
    for(int i = 0; i < ArrayLenth; i++){
        // ESP32-S3的analogRead默认返回12位值(0-4095)
        val[i] = analogRead(pin);
        delay(20);
    }
    
    // 排序
    for(int i = 0; i < ArrayLenth - 1; i++){
        int temp = 0;
        for(int j = i + 1; j < ArrayLenth; j++){
            if(val[i] > val[j]){
                temp = val[i];
                val[i] = val[j];
                val[j] = temp;
            }
        }
    }
    float averageVoltage = getMedianNum(val, ArrayLenth) * VREF / 4096.0; 
    doValue = SaturationValueTab[(int)(temperature + 0.5)] * averageVoltage / (SaturationDoVoltage / 1000.0);
    
    return doValue;
}
